package com.baidu.automata.core.type;

import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import org.dom4j.Element;
import org.w3c.dom.Node;

import com.baidu.common.util.FieldUtil;

public class MapField extends DataField implements Map<String, DataField> {
	private Map<String, DataField> value = new HashMap<String, DataField>();

	public MapField() {
		super();
	}

	public MapField(String id) {
		super(id);
	}

	@Override
	public int size() {
		return this.value.size();
	}

	@Override
	public boolean isEmpty() {
		return this.value.isEmpty();
	}

	@Override
	public boolean containsKey(Object key) {
		return this.value.containsKey(key);
	}

	@Override
	public boolean containsValue(Object value) {
		return this.value.containsValue(value);
	}

	@Override
	public DataField get(Object key) {
		return this.value.get(key);
	}

	@Override
	public DataField put(String key, DataField value) {
		return this.value.put(key, value);
	}

	@Override
	public DataField remove(Object key) {
		return this.value.remove(key);
	}

	@Override
	public void putAll(Map<? extends String, ? extends DataField> m) {
		this.value.putAll(m);
	}

	@Override
	public void clear() {
		this.value.clear();
	}

	@Override
	public Set<String> keySet() {
		return this.value.keySet();
	}

	@Override
	public Collection<DataField> values() {
		return this.value.values();
	}

	@Override
	public Set<java.util.Map.Entry<String, DataField>> entrySet() {
		return this.value.entrySet();
	}

	@SuppressWarnings("unchecked")
	@Override
	public void deserialize(Element e) throws Exception {
		this.id = e.attributeValue("id");
		for (Iterator<Element> it = e.elementIterator(); it.hasNext();) {
			Element emt = it.next();
			DataField f = FieldUtil.initFiled(emt);
			this.value.put(f.getId(), f);
		}
	}

	@Override
	public Node serialize() throws Exception {
		return null;
	}
}
